package com.kingyea.app.market.versionInfo.controller;

import com.alibaba.fastjson.JSON;
import com.kingyea.app.market.client.UserServiceClient;
import com.kingyea.app.market.syncMission.bean.SyncMission;
import com.kingyea.app.market.syncMission.bean.SyncMission.PushStatus;
import com.kingyea.app.market.syncMission.service.SyncMissionService;
import com.kingyea.app.market.utils.page.PageInfo;
import com.kingyea.app.market.versionInfo.bean.VersionInfo;
import com.kingyea.app.market.versionInfo.service.VersionInfoService;
import com.kingyea.mobilepolice.system.Result;
import com.kingyea.mobilepolice.user.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version v1.0.0
 * @description VersionInfo 控制层
 * @date Created in 2018-02-11 11:43:16
 */
@RequestMapping(value = "/versionInfo")
@RestController
public class VersionInfoController {

    private static final Logger LOGGER = LoggerFactory.getLogger(VersionInfoController.class);

    @Autowired
    private VersionInfoService versionInfoService;
    @Autowired
    private SyncMissionService syncMissionService;
    @Autowired
    private UserServiceClient userServiceClient;

    /**
     * 查询全部
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list(@RequestBody Map<String, Object> version) throws Exception {
        return versionInfoService.list(version);
    }

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public VersionInfo detail(@PathVariable String id) throws Exception {
        return versionInfoService.getById(id);
    }

    /**
     * 创建
     *
     * @param versionInfo
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Object create(@RequestBody VersionInfo versionInfo) {
        Result<User> currentUserInfo = userServiceClient.getCurrentUserInfo();
        if (currentUserInfo.getData() != null) {
            User data = currentUserInfo.getData();
            versionInfo.setCreater(data.getName());
            versionInfo.setCreateDate(new Date());
        }
        VersionInfo save = null;
        try {
            save = versionInfoService.save(versionInfo);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return save;
    }

    /**
     * 更新
     *
     * @param versionInfo
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(@RequestBody VersionInfo versionInfo) {
        VersionInfo update = null;
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("appCode", versionInfo.getAppCode());
//        	map.put("version", versionInfo.getVersion());
            map.put("versionType", versionInfo.getVersionType());
            PageInfo<VersionInfo> list = versionInfoService.list(map);
            List<VersionInfo> versionList = JSON.parseArray(JSON.toJSONString(list.getList()), VersionInfo.class);
            for (VersionInfo versionInfo2 : versionList) {
                if (!StringUtils.isEmpty(versionInfo2.getIsUseful())) {
                    if (versionInfo2.getIsUseful() == 1) {//true  已发布版本，信息修改需生成同步任务
                        //应用发布后，对版本信息修改，生成同步任务
                        SyncMission syncMission = new SyncMission();
                        syncMission.setAppCode(versionInfo.getAppCode());
                        syncMission.setVersion(versionInfo.getVersion());
                        syncMission.setVersionType(versionInfo.getVersionType());
                        syncMission.setStatus(PushStatus.waiting);
                        syncMission.setStartDate(new Date(System.currentTimeMillis()));
                        syncMissionService.save(syncMission);

                    }
                }
            }
//        	VersionInfo versionInfo2 = versionInfoService.getById(versionInfo.getId());
            update = versionInfoService.update(versionInfo);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return update;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public int delete(@PathVariable String id) throws Exception {
        return versionInfoService.deleteById(id);
    }

    @RequestMapping(value = "/list/{versionType}", method = RequestMethod.GET)
    public List<VersionInfo> getListByVersionType(@PathVariable String versionType) {
        List<VersionInfo> listByVersionType = null;
        try {

            listByVersionType = versionInfoService.getListByVersionType(versionType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listByVersionType;

    }

    /**
     * 获取应用版本信息
     *
     * @param appBaseInfo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/versionList", method = RequestMethod.POST)
    public Object versionList(@RequestBody Map<String, Object> appBaseInfo) throws Exception {
        return versionInfoService.versionList(appBaseInfo);
    }

}
